package com.sgb.zhaocai.view.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import com.google.android.material.tabs.TabLayout;
import com.sgb.link.entity.PickInvitedApply;
import com.sgb.zhaocai.R;
import com.sgb.zhaocai.databinding.ActivityPickInvitedSignupBinding;
import com.sgb.zhaocai.entity.FindRuleBean;
import com.sgb.zhaocai.view.fragment.PickMarketUserFragment;
import com.sgb.zhaocai.view.fragment.PickMemberUnitFragment;
import com.sgb.zhaocai.viewmodel.PickInvitedSignUpModel;
import com.swgk.core.base.AppActivity;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Time:2021/12/20
 * AUTHOR:F_Q
 * Description: 邀请报名-邀请竞价界面
 */
public class PickInvitedSignUpActivity extends AppActivity {

    private static String FROMWHERE = "fromWhere";
    private static String DATA = "data";
    private ActivityPickInvitedSignupBinding mBinding;
    private PickInvitedSignUpModel mModel;
    private String fromWhere = "";
    private ArrayList<String> titles;
    private ArrayList<Fragment> mFragments;
    private PickInvitedApply bean;
    private boolean ShowTwoTab;//是否显示二级tab
    private boolean ShowTwoCompanyList;//是否只加载企业列表
    private int position = 0;

    //仅公司、公司及个人、是否限会员、仅公司和会员时
    public static void start(Context context, PickInvitedApply pickInvitedApply, String fromWhere) {
        Intent intent = new Intent();
        intent.setClass(context, PickInvitedSignUpActivity.class);
        intent.putExtra(DATA, (Serializable) pickInvitedApply);
        intent.putExtra(FROMWHERE, fromWhere);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_pick_invited_signup);
        mModel = new ViewModelProvider(this).get(PickInvitedSignUpModel.class);
        mModel.setActivity(PickInvitedSignUpActivity.this);
        fromWhere = getIntent().getStringExtra(FROMWHERE);
        bean = (PickInvitedApply) getIntent().getSerializableExtra(DATA);
        mBinding.includeTitle.ivBack.setOnClickListener(v -> finish());
        mBinding.includeTitle.titleView.setText(TextUtils.equals(fromWhere, "1") ? "邀请报名" : "邀请竞价");
        mBinding.includeTitle.rightView.setText("邀请非网站");
        //跳转到邀请非网站
        mBinding.includeTitle.rightView.setOnClickListener(v -> PickInvitedTheWebSiteActivity.start(getActivity(), bean, fromWhere));
        //搜索
        mBinding.tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentListData();
            }
        });
        mBinding.editSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 1) {
                    mBinding.searchDelete.setVisibility(View.VISIBLE);
                } else {
                    mBinding.searchDelete.setVisibility(View.GONE);
                }
            }
        });
        mBinding.searchDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mBinding.editSearch.setText("");
                mBinding.searchDelete.setVisibility(View.GONE);
                FragmentListData();
            }
        });
    }

    @Override
    protected void init() {
        super.init();
        //获取招标竞价规则
        if (TextUtils.equals(fromWhere,"1")){
            mModel.findTenderRuleDetail(bean.getId());
        }else {
            mModel.findBidderRuleDetail(bean.getId());
        }
    }

    /**
     * 招标竞价规则
     * @param data
     */
    public void setFindRule(FindRuleBean data) {
        if (data!=null){
            tabViewData(data.getBiddingUserSign(),data.getBiddingUserIdentity());
        }else {
            tabViewData(bean.getBiddingUserSign(),bean.getBiddingUserIdentity());
        }
    }

    private void tabViewData(List<Integer> biddingusersign,String BiddingUserIdentity) {
        List<Integer> biddingUserSign = biddingusersign;//限定
        if (!TextUtils.isEmpty(BiddingUserIdentity)) {
            //身份字段
            switch (BiddingUserIdentity) {
                case "1"://公司、个人皆可，
                    //会员单位和市场客户tab展示，同时也展示企业和个人标签
                    if (biddingUserSign != null && biddingUserSign.size() > 0 && biddingUserSign.contains(2)) {
                        //公司、个人皆可，切限定会员，只加载会员单位下的企业列表及个人列表，不展示会员单位和市场客户
                        tabView(1);
                    } else {
                        //仅公司切不限定会员，所有tab都显示
                        tabView(2);
                    }
                    break;
                case "2"://仅限公司,
                    if (biddingUserSign != null && biddingUserSign.size() > 0 && biddingUserSign.contains(2)) {
                        //仅公司切限定会员，只加载会员单位的企业列表，其他tab都不展示
                        tabView(3);
                    } else {
                        //仅公司切不限定会员，只展示会员单位和市场客户tab，企业和个人tab不显示，默认加载企业数据
                        tabView(4);
                    }
                    break;
                case "3"://限会员
                  //  tabView(1);
                    break;
            }
        } else {
            //仅公司切不限定会员，所有tab都显示
            tabView(2);
        }
    }

    public void tabView(int type) {
        titles = new ArrayList<>();
        mFragments = new ArrayList<>();
        switch (type) {
            case 1://2.3 只加载会员单位下的企业列表及个人列表，不展示会员单位和市场客户
                titles.add("会员单位");
                ShowTwoTab = true;//二级tab显示
                ShowTwoCompanyList = false;//企业和个人tab都展示
                mFragments.add(PickMemberUnitFragment.newInstance(ShowTwoTab, ShowTwoCompanyList, bean, fromWhere));//二级tab都展示
                mBinding.tabLayout.setVisibility(View.GONE);
                break;
            case 2://2.2 所有tab都显示
                titles.add("会员单位");
                titles.add("市场客户");
                ShowTwoTab = true;//二级tab显示
                ShowTwoCompanyList = false;//企业和个人tab都展示
                mFragments.add(PickMemberUnitFragment.newInstance(ShowTwoTab, ShowTwoCompanyList, bean, fromWhere));//二级tab展示，
                mFragments.add(PickMarketUserFragment.newInstance(ShowTwoTab, ShowTwoCompanyList, bean, fromWhere));//二级tab展示，
                mBinding.tabLayout.setVisibility(View.VISIBLE);
                break;
            case 3://2.4 只加载会员单位的企业列表，其他tab都不展示
                titles.add("会员单位");
                ShowTwoTab = false;//二级tab显示
                ShowTwoCompanyList = true;//企业和个人tab都不展示，默认加载企业
                mFragments.add(PickMemberUnitFragment.newInstance(ShowTwoTab, ShowTwoCompanyList, bean, fromWhere));//二级tab不展示，默认加载企业
                mBinding.tabLayout.setVisibility(View.GONE);
                //二级tab不展示只展示企业
                break;
            case 4://2.1 只展示会员单位和市场客户tab，企业和个人tab不显示，默认加载企业数据
                titles.add("会员单位");
                titles.add("市场客户");
                ShowTwoTab = false;//二级tab显示
                ShowTwoCompanyList = true;//企业和个人tab都不展示，默认加载企业
                mFragments.add(PickMemberUnitFragment.newInstance(ShowTwoTab, ShowTwoCompanyList, bean, fromWhere));//二级tab不展示，默认加载企业
                mFragments.add(PickMarketUserFragment.newInstance(ShowTwoTab, ShowTwoCompanyList, bean, fromWhere));//二级tab不展示，默认加载企业
                mBinding.tabLayout.setVisibility(View.VISIBLE);
                //二级tab不展示只展示企业
                break;

        }
        String[] strings = new String[titles.size()];
        String[] mTitles = titles.toArray(strings);
        SlidingFragmentPagerAdapter mPagerAdapter = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), mFragments, mTitles);
        mBinding.viewPager.setOffscreenPageLimit(mTitles.length);
        mBinding.viewPager.setAdapter(mPagerAdapter);
        mBinding.tabLayout.setupWithViewPager(mBinding.viewPager);
        //不可滑动设置
        mBinding.viewPager.setNoScroll(true);
        updateTabTextView(mBinding.tabLayout.getTabAt(mBinding.tabLayout.getSelectedTabPosition()), true);
        mBinding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                updateTabTextView(tab, true);
                position = tab.getPosition();
                //设置搜索默认文案
                EditTextHiet();
                //为了实现实时搜索调用次方法，点击一次搜索按钮，切换tab数据展示搜索结果数据实现
                FragmentListData();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                updateTabTextView(tab, false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

    //设置文案
    private void EditTextHiet() {
        Fragment fragment = mFragments.get(position);
        if (fragment != null && fragment instanceof PickMemberUnitFragment) {
            if (TextUtils.isEmpty(mBinding.editSearch.getText().toString().trim())) {
                mBinding.editSearch.setHint("请输入会员单位名称");
            }
        }
        if (fragment != null && fragment instanceof PickMarketUserFragment) {
            if (TextUtils.isEmpty(mBinding.editSearch.getText().toString().trim())) {
                ((PickMarketUserFragment) fragment).setHiet(mBinding.editSearch);
            }
        }
    }

    private void FragmentListData() {
        Fragment fragment = mFragments.get(position);
        if (fragment != null && fragment instanceof PickMemberUnitFragment) {
            ((PickMemberUnitFragment) fragment).initSearch(TextUtils.isEmpty(mBinding.editSearch.getText().toString().trim()) ? "" : mBinding.editSearch.getText().toString().trim());
        }
        if (fragment != null && fragment instanceof PickMarketUserFragment) {
            ((PickMarketUserFragment) fragment).initSearch(TextUtils.isEmpty(mBinding.editSearch.getText().toString().trim()) ? "" : mBinding.editSearch.getText().toString().trim());
        }
    }

    private void updateTabTextView(TabLayout.Tab tab, boolean isSelect) {
        if (tab == null || tab.getCustomView() == null) return;
        if (isSelect) {
            //选中加粗
            TextView tabSelect = (TextView) tab.getCustomView().findViewById(R.id.tv_tab_item_textview);
            tabSelect.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            tabSelect.setTextColor(getResources().getColor(R.color.color_main));
            tabSelect.setTextSize(14);
            tabSelect.setText(tab.getText());
        } else {
            TextView tabUnSelect = (TextView) tab.getCustomView().findViewById(R.id.tv_tab_item_textview);
            tabUnSelect.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            tabUnSelect.setTextColor(getResources().getColor(R.color.color_tab_one));
            tabUnSelect.setTextSize(14);
            tabUnSelect.setText(tab.getText());
        }
    }
}
